<?php
class kick extends command {

	public function __construct(){
		$this->name = 'kick';
		$this->public = false;
		$this->channels = array('#juchipila');
	}

	public function help(){
		return "Uso: !kick <nick> <reason> | <offending text>. Expulsa al usuario especificado.";
	}	
	
	public function process($args){
		if ( ! $this->issuedbyadmin){
			return 0;
		}
		$this->output = "";
		global $db;
		$args = trim($args);
		$nick = substr($args, 0, strpos($args, " "));
		$args = substr($args, strlen($nick));
		$args = trim($args);
		list($reason, $offendingtext) = split("\|", $args);
		$reason = trim($reason);
		$offendingtext = trim($offendingtext);
		
		//obtain the last ip used by the nick being kicked
		$sql = "select 
					ip 
				from 
					chatlogins 
				where 
					nick = :nick 
					and 
					logoutdate = '0000-00-00 00:00:00'
				order by
					loginid DESC
				limit 1";
		$r = $db->Parse($sql, 1);
		$r->Bind(":nick", $nick);
		$r->Execute();
		$row = $r->GetRow();		
		
		if ( empty($row)){
			$errormsg = "No se pudo encontrar la ip del usuario.";
			fwrite($this->socket, "PRIVMSG " . $this->nick . " :" . $errormsg . "\n");
		} else {
			$ip = $row->ip;
			$sql = "insert into chatkicks 
					(ip, nick, kickdate, reason, offendingtext) 
					values 
					(:ip, :nick, now(), :reason, :offendingtext)";
			$r = $db->Parse($sql, 1);
			$r->Bind(":ip", $ip);
			$r->Bind(":nick", $nick);
			$r->Bind(":reason", $reason);
			$r->Bind(":offendingtext", $offendingtext);			
			$r->Execute();
			
			if ( $r->RowCount()){
				$msg = "kick agregado al sistema.";
				fwrite($this->socket, "PRIVMSG " . $this->nick . " :" . $msg . "\n");
				fwrite($this->socket,"KICK {$this->channel} {$nick} {$reason}\n");
			} else {
				$msg = "No se pudo agregar el kick al sistema.";
				fwrite($this->socket, "PRIVMSG " . $this->nick . " :" . $msg . "\n");
			}
		}
	}
}
?>